'use strict';
import React, {Component} from 'react';

import {
    View,
    Image,
    ImageBackground,
    Dimensions,
    TouchableWithoutFeedback,
    StyleSheet,
    Text,
    TouchableOpacity,
    TextInput,
    ScrollView,
    TouchableHighlight,
    TouchableNativeFeedback,
    Switch,
    Modal
} from 'react-native';

import {connect} from 'react-redux'
import Loading from '../../../../../core/component/comp.global-loading';
import Color from '../../../../../core/constant/color';
import Toolbar from '../../../../../core/component/comp.toolbar';
import InitDB from '../../../../../core/database/init-db';
import ModalDropdown from '../../../../../core/component/comp.modal-dropdown';
import TableRowItem from '../../../../../core/component-biz/comp.table-row-input-item';
import {NavigationActions} from 'react-navigation'
import ImageButton from "../../../../../core/component/comp.image-button";
import IconButton from "../../../../../core/component/comp.icon-button";
import Util from "../../../../../core/utils/util";
import {userActions} from "../../../../../actions"

let winWidth = Dimensions.get('window').width;//屏幕宽度
let winHeight = Dimensions.get('window').height;//屏幕高度

class ChangePwdScene extends React.Component {

    static navigationOptions = ({navigation, screenProps}) => ({
        title: "修改密码",
        headerTintColor: "white",
        tabBarVisible: false,
        headerStyle: {
            backgroundColor: Color.PRIMARY_COLOR,
            elevation: null

        },
    });

    // 构造
    constructor(props) {
        super(props);
        this.state = {
            showGlobalLoading:false,
            oldPwd: '',
            newPwd: '',
            newPwd2: '',
        }
    }


    onPressSubmit = () => {
        const {dispatch} = this.props;
        const {oldPwd, newPwd, newPwd2} = this.state;
        if (oldPwd === '' || newPwd === '' || newPwd2 === '') {
            Util.showToast("密码不能为空")
            return
        }

        if (newPwd !== newPwd2) {
            Util.showToast("两次密码不同")
            return
        }

        if (oldPwd === newPwd) {
            Util.showToast("新旧密码不能相同")
            return
        }

        Util.showGlobalLoading(this)
        //发送密码到服务器修改
        dispatch(userActions.changePassword(oldPwd,newPwd)).then((json)=>{
            if(json.returnType.resultCode=="1"){
                Util.showToast("密码修改成功！")
                this.props.navigation.goBack();
            }else{
                Util.showToast(json.returnType.resultMessage)
            }
            Util.hiddenGlobalLoading(this)
        }).catch(()=>{
            Util.hiddenGlobalLoading(this)
        })
    }

    onChangeInput(key, text) {
        this.setState({
            [key]: text,
        })
    }

    render() {
        const {oldPwd, newPwd, newPwd2,showGlobalLoading} = this.state;
        return (
            <View>
                <Loading show={showGlobalLoading}/>
                <ScrollView style={styles.body}>
                    <TouchableWithoutFeedback
                        onPress={() => {
                            this.setState({modalVisible: '0'});
                        }}>
                        <View>
                            <TableRowItem placeHolder='请输入旧密码' value={oldPwd}
                                          onChangeText={this.onChangeInput.bind(this, 'oldPwd')}/>
                            <TableRowItem placeHolder='请输入新密码' value={newPwd}
                                          onChangeText={this.onChangeInput.bind(this, 'newPwd')}/>
                            <TableRowItem placeHolder='再次输入新密码' value={newPwd2}
                                          onChangeText={this.onChangeInput.bind(this, 'newPwd2')} showSplit={false}/>
                            <IconButton title='确定' style={styles.btnSubmit} onPress={this.onPressSubmit}/>
                        </View>
                    </TouchableWithoutFeedback>
                </ScrollView>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    body: {
        backgroundColor: Color.VIEW_BG_COLOR,
        height: winHeight,
    },
    btnSubmit: {
        margin: 8,
    }
});

export default connect((state, props) => {
    return ({

    });
})(ChangePwdScene);